﻿using UnityEngine;
using System.Collections;

public class EnemyController : MonoBehaviour {

	public float jumpForce = 12;
	[HideInInspector] private float jumpHeight = 18f/5f; // MAGIC NUMBER : This is how high the enemy jumps with jumpForce 12. This should be calculated using jumpForce.

	public float MovementSpeed;

	public EnemyPerceptions perceptions;

	private Vector2 amountToMove;
	private float gravity = 20;
	private EnemyPhysics enemyPhysics;
	private Transform player;
	
	// Use this for initialization
	void Start () {
		enemyPhysics = GetComponent<EnemyPhysics>();

	}
	
	// Update is called once per frame
	void Update () {
		float dir = 0;

		if (perceptions.canSeePlayer || perceptions.RememberPlayerPosition()) {
			dir = Mathf.Sign(perceptions.lastPlayerPosition.x - transform.position.x);
		}

		if (enemyPhysics.grounded && enemyPhysics.isAtBorder) {

			// Fall off ledge if can return
//			if (enemyPhysics.ledgeHeight < jumpHeight) {
//				enemyPhysics.DropDown();
//			}

			// Jump
//			amountToMove.y = jumpForce;

			// Fall off ledge
//			enemyPhysics.DropDown();
		}

		if (enemyPhysics.grounded) {
			amountToMove.y = 0;
		}
		amountToMove.x = dir * MovementSpeed;
		amountToMove.y -= gravity * Time.deltaTime;

		enemyPhysics.Move(amountToMove * Time.deltaTime);

	}

	void FixedUpdate() {
		perceptions.canSeePlayer = CheckPlayerInSight();
	}

	bool CheckPlayerInSight() {
		bool result = false;
		if (player == null) {
			player = GameObject.FindGameObjectWithTag("Player").transform;
		}
		Collider playerCollider = player.collider;

		RaycastHit hitInfo;
		Debug.DrawLine(transform.position, (playerCollider.bounds.center + new Vector3(0, playerCollider.bounds.size.y / 2, 0)), Color.red);
		if (Physics.Raycast(transform.position, (playerCollider.bounds.center + new Vector3(0, playerCollider.bounds.size.y / 2, 0)) - transform.position, out hitInfo, perceptions.sightDistance)) {
			if (hitInfo.collider.gameObject.tag == "Player") {
				Debug.DrawLine(transform.position, (playerCollider.bounds.center + new Vector3(0, playerCollider.bounds.size.y / 2, 0)), Color.green);
				result = true;
			}
		}

		Debug.DrawLine(transform.position, (playerCollider.bounds.center - new Vector3(0, playerCollider.bounds.size.y / 2, 0)), Color.red);
		if (Physics.Raycast(transform.position, (playerCollider.bounds.center - new Vector3(0, playerCollider.bounds.size.y / 2, 0)) - transform.position, out hitInfo, perceptions.sightDistance)) {
			if (hitInfo.collider.gameObject.tag == "Player") {
				Debug.DrawLine(transform.position, (playerCollider.bounds.center - new Vector3(0, playerCollider.bounds.size.y / 2, 0)), Color.green);
				result = true;
			}
		}
		if (result) {
			perceptions.lastPlayerPosition = player.gameObject.collider.bounds.center;
			perceptions.lastTimePlayerSeen = 0;
		} else {
			perceptions.lastTimePlayerSeen += Time.fixedDeltaTime;
		}

		if (perceptions.RememberPlayerPosition()) {
			Debug.DrawLine(transform.position, perceptions.lastPlayerPosition, Color.black);
		}

		return result;
	}

	void OnDrawGizmosSelected() {
		Gizmos.color = Color.green;
		Gizmos.DrawWireSphere(transform.position, perceptions.sightDistance);
	}
}
